package com.tbit.main.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserBound;
import com.tbit.main.pojo.Greatops;
import com.tbit.main.pojo.GreatopsMachine;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 高效运维
 *
 * @author LMY
 * @create 2020-11-28 13:47
 */
@RestController
@RequestMapping("/greatops")
public class GreatopsController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private GreatopsService greatopsService;


    /**
     * 查询
     *
     * @param token
     * @param greatops
     * @param regionId
     * @return
     */
    @RequestMapping(value = "/query")
    public JsonResult query(String token, Greatops greatops,
                            @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        Assert.notNull(greatops.getAccountId(), "AccountUser.agentId.notNull");
        AccountUser accountUserToken = tokenService.getAccount(token);
        if (greatops.getMapType() == null) {
            greatops.setMapType(SysConstant.GOOGLE);
        }

        //删除用户，修改用户角色检测
        AccountUserBound accountUserBound = accountUserService.getBoundByUserId(accountUserToken.getAccountUserId());
        if (accountUserBound == null) {
            /**删除token*/
            redisService.del(token);
            throw new BaseException("AccountUser.notExist");
        } else {
            if (!accountUserBound.getSubRoleType().equals(accountUserToken.getSubRoleType())) {
                /**删除token*/
                redisService.del(token);
                throw new BaseException("System.login.need");
            }
        }

        Assert.notNull(greatops.getPowerSwitch(), "GreatopsController.PowerSwitch.notNull");
        Assert.notNull(greatops.getIdleTimeSwitch(), "GreatopsController.idleTimeSwitch.notNull");
        Assert.notNull(greatops.getFaultSwitch(), "GreatopsController.FaultSwitch.notNull");
        Assert.notNull(greatops.getAbnormalSwitch(), "GreatopsController.AbnormalSwitch.notNull");
        Assert.notNull(greatops.getOccupySwitch(), "GreatopsController.occupySwitch.notNull");

        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUserToken.getAccountUserId());
        }

        GreatopsMachine machineGreatops = greatopsService.getMachineStatus(greatops, regionId);

        return JsonResult.succ(machineGreatops);
    }

}
